package controller;

import javax.servlet.annotation.WebServlet;
import java.io.IOException;


@WebServlet("/JSTLServlet")
public class Wudengyu extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {

    }

    protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        String action = request.getParameter("action");
        response.setContentType("text/html;charset=utf-8");
        if ("".equals(action) || action == null) {
            response.getWriter()
                    //场景一 给用户提供功能
                    .append("本页面是程序首页,为浏览用户提供了若干功能<br>")
                    .append("在URL后添加<font color='red'>查询字符</font>,测试EL内置对象<br/>")
                    .append("?action=scope 使用EL获取你并输出4个对象的属性值</br>")
                    .append("?action=param 使用EL获取并输出请求参数值<br/>")
                    .append("?action=bean 使用EL获取JavaBean命名属性的值<br/>")
                    .append("?action=pageContext 使用EL获取pageContext对象<br/>")
                    .append("?action=header 使用EL获取HTTP请求头和cookie请求头<br/>")
                    .append("?action=empty empty运算符的使用<br/>")
                    .append("?action=foreach JSTL循环的使用<br/>");
            return;
        }
        //场景二 获取4个域对象的属性
        if ("scope".equals(action)) {
            request.setAttribute("userName", "张三request");
            javax.servlet.http.HttpSession httpSession = request.getSession();
            httpSession.setAttribute("userName", "张三 session");
            javax.servlet.ServletContext servletContext = request.getServletContext();
            servletContext.setAttribute("userName", "张三application");
            request.getRequestDispatcher("/scope.jsp").include(request, response);
            return;
        }

        //场景三 使用EL获取并输出请求参数
        if ("param".equals(action)) {
            String name = java.net.URLEncoder.encode("张三", "UTF-8");
            String paramPath = "/param.jsp?userName=" + name + "&hobby=shopping&hobby=music";
            request.getRequestDispatcher(paramPath).include(request, response);
            return;

        }


    }
}








